<?php  
defined("C5_EXECUTE") or die(_("Access Denied."));
class ProcessingGalleryBlockController extends BlockController {

	private $codes = array();
	private $images = array();
	private $result = array();
	
	protected $btTable = "btprocessingGallery";
	protected $btInterfaceWidth = "550";
	protected $btInterfaceHeight = "400";

	public function getBlockTypeDescription() {
		return t("Processing Gallery");
	}
	
	public function getBlockTypeName() {
		return t("Processing Gallery");
	}
	
	public function getJavaScriptStrings() {
		return array(
			"choose-file"		=> t("Choose Image/File"),
			"choose-min-2"	=> t("Please choose at least two images."),
			"choose-fileset"	=> t("Please choose a file set.")
		);
	}
	
	function __construct($obj = null) {		
		parent::__construct($obj);
		$this->db = Loader::db();
//		if ($this->fsID == 0) {
//			$this->loadImages();
//		} else {
			$this->loadFileSet();
//		}
//		$this->set('minHeight', $this->minHeight);
		$this->set("fsID", $this->fsID);
		$this->set("fsName", $this->getFileSetName());
		$this->set("result", $this->result);
		$this->set("codes", $this->codes);
		$this->set("canvasHeight",	$this->canvasHeight);
		$this->set("canvasWidth",	$this->canvasWidth);

//		$type = ($this->fsID > 0) ? 'FILESET' : 'CUSTOM';
//		$this->set('type', $type);
		$this->set('bID', $this->bID);			
	}	
	
	public function on_page_view() {
		$html = Loader::helper('html');
		$b = $this->getBlockObject();
        $bv = new BlockView();
        $bv->setBlockObject($b);

		$query  = "?";
		$query .= "id=processing".$this->bID."&amp;";
		$query .= "height=".$this->canvasHeight."&amp;";
		$query .= "width=".$this->canvasWidth."&amp;";
		$query .= "url=".$bv->getBlockURL();

         $jsPath =  "<script type=\"text/javascript\" src=\"".$bv->getBlockURL()."/processing.as.js".$query."\"></script>"; 
		 $this->addHeaderItem($jsPath);

         $jsPath =  "<script type=\"text/javascript\" src=\"".$bv->getBlockURL()."/processing.js\"></script>"; 
		 $this->addHeaderItem($jsPath);
	}
	
	function getFileSetName(){
		$sql = "SELECT fsName FROM FileSets WHERE fsID=".intval($this->fsID);
		return $this->db->getOne($sql); 
	}

	function loadFileSet(){
		if (intval($this->fsID) < 1) { return false; }

		Loader::library("file/types");
		Loader::helper("concrete/file");
		Loader::model("file_attributes");
		Loader::model("file_list");
		Loader::model("file_set");
		Loader::model("file_version");

		$h = Loader::helper('file');
		
		$fs = FileSet::getByID($this->fsID);
		$fileList = new FileList();		
		$fileList->filterBySet($fs);
		$files = $fileList->get(1000,0);				

		foreach ($files as $f) {
			$fp = new Permissions($f);
			if(!$fp->canRead()) { continue; }

			$extension = $h->getExtension($f->getPath());
			switch($extension) {
				case "jpg":
				case "gif":
				case "png":
					$this->addImage($f);
					break;
				case "pde":
					$this->addCode($f);
					break;
			}
		}
		$this->parse();
	}

	function addCode($f) {
		$code = array();

		$vp = $f->getRecentVersion();
		$code["fID"]				= $f->getFileID();
		$code["fileName"] 		= $f->getFileName();
		$code["fullFilePath"]	= $f->getPath();
		$code["url"]					= $f->getRelativePath();
		$code["description"]	= $vp->getDescription();
		$this->codes[] = $code;
	}

	function addImage($f) {
		$image = array();
		$image["fID"]					= $f->getFileID();
		$image["fileName"] 		= $f->getFileName();
		$image["fullFilePath"]	= $f->getPath();
		$image["url"]					= $f->getRelativePath();

		$key = str_replace(array(".jpg",".gif",".png"), array("","",""), $image["fileName"]);
		$this->images[$key] = $image;
	}
	
	
	function parse() {
		$this->result = array();
		$codes = $this->codes;
		foreach($codes as $key => $code) {
			$c = file_get_contents($code["fullFilePath"]);
			preg_match_all("/loadImage\(\"(.*)\"\);/", $c, $matches, PREG_OFFSET_CAPTURE);

			$images = array();
			$subMatch = array_pop($matches);
			foreach ($subMatch as  $r) {
				$name	= array_shift($r);
				$key = str_replace(array(".jpg",".gif",".png"), array("","",""), $name);
				$image = $this->images[$key];
				$images[$key] =  array(
					"name" => $name,
					"url" 	=> $image["url"]
				);
			}

			$thumbkey = str_replace(".pde", "", $code["fileName"])."_thumbnail";
			$thumbUrl = (empty($this->images[$thumbkey])) ? "": $this->images[$thumbkey]["url"];
			$parameters = array(
				"thumbnail"=> $thumbUrl,
				"code" => $code["url"],
				"description" => $code["description"],
				"images"	=> $images
			);
			$this->result[] = $parameters; 
		}
	}
}

?>
